Skip to content

Commit d7b8025

Browse files
committed
[Clang][Sema] Add warning when converting bool to character types (with exception of explicitly qualified types, which are aliases to int8_t and uint8_t)
1 parent fb13be0 commit d7b8025

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

clang/include/clang/AST/Type.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2527,6 +2527,8 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
25272527
bool isAnyCharacterType() const;
25282528
bool isUnicodeCharacterType() const;
25292529
bool isIntegralType(const ASTContext &Ctx) const;
2530+
bool isExplicitlyQualifiedCharType() const;
2531+
bool isUnqualifiedCharType() const;
25302532

25312533
/// Determine whether this type is an integral or enumeration type.
25322534
bool isIntegralOrEnumerationType() const;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4348,6 +4348,9 @@ def warn_impcast_constant_value_to_objc_bool : Warning<
43484348
"the only well defined values for 'BOOL' are YES and NO">,
43494349
InGroup<ObjCBoolConstantConversion>;
43504350

4351+
def warn_impcast_bool_to_character : Warning<
4352+
"implicit conversion from 'BOOL' to %0">, InGroup<Conversion>;
4353+
43514354
def warn_impcast_fixed_point_range : Warning<
43524355
"implicit conversion from %0 cannot fit within the range of values for %1">,
43534356
InGroup<ImplicitFixedPointConversion>;

clang/lib/AST/Type.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,6 +2145,20 @@ bool Type::isCharType() const {
21452145
return false;
21462146
}
21472147

2148+
bool Type::isExplicitlyQualifiedCharType() const {
2149+
if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
2150+
return BT->getKind() == BuiltinType::UChar ||
2151+
BT->getKind() == BuiltinType::SChar;
2152+
return false;
2153+
}
2154+
2155+
bool Type::isUnqualifiedCharType() const {
2156+
if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
2157+
return BT->getKind() == BuiltinType::Char_U ||
2158+
BT->getKind() == BuiltinType::Char_S;
2159+
return false;
2160+
}
2161+
21482162
bool Type::isWideCharType() const {
21492163
if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
21502164
return BT->getKind() == BuiltinType::WChar_S ||

clang/lib/Sema/SemaChecking.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11968,6 +11968,23 @@ static const IntegerLiteral *getIntegerLiteral(Expr *E) {
1196811968
return IL;
1196911969
}
1197011970

11971+
static void DiagnoseFromBoolConversions(Sema &S, QualType T, Expr *E) {
11972+
E = E->IgnoreParenImpCasts();
11973+
SourceLocation ExprLoc = E->getExprLoc();
11974+
11975+
const Type *Target = S.Context.getCanonicalType(T).getTypePtr();
11976+
if (Target->isAnyCharacterType() &&
11977+
!Target->isExplicitlyQualifiedCharType()) {
11978+
if (S.LangOpts.CPlusPlus) {
11979+
S.Diag(ExprLoc, diag::warn_impcast_bool_to_character)
11980+
<< T
11981+
<< FixItHint::CreateInsertion(E->getBeginLoc(), "static_cast<bool>(")
11982+
<< FixItHint::CreateInsertion(S.getLocForEndOfToken(E->getEndLoc()),
11983+
")");
11984+
}
11985+
}
11986+
}
11987+
1197111988
static void DiagnoseIntInBoolContext(Sema &S, Expr *E) {
1197211989
E = E->IgnoreParenImpCasts();
1197311990
SourceLocation ExprLoc = E->getExprLoc();
@@ -12434,6 +12451,9 @@ void Sema::CheckImplicitConversion(Expr *E, QualType T, SourceLocation CC,
1243412451
if (Target->isBooleanType())
1243512452
DiagnoseIntInBoolContext(*this, E);
1243612453

12454+
if (Source->isBooleanType())
12455+
DiagnoseFromBoolConversions(*this, T, E);
12456+
1243712457
if (DiscardingCFIUncheckedCallee(QualType(Source, 0), QualType(Target, 0))) {
1243812458
Diag(CC, diag::warn_cast_discards_cfi_unchecked_callee)
1243912459
<< QualType(Source, 0) << QualType(Target, 0);

0 commit comments

Comments
 (0)